/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service.processor.active;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.customer.core.domain.Customer;
import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.service.processor.base.AbstractCustomerServiceProcessor;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

import java.util.Optional;

/**
 * <b>(CustomerActivePartnerProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/15
 */
@SuppressWarnings("rawtypes")
@Component
public class CustomerActivePartnerProcessor extends AbstractCustomerServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 55;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        Customer customer = (Customer) serviceContext.getDomain();

        // 自来水则默认平台为合作伙伴
        String  partnerId = Optional.ofNullable(customer.getPartnerId()).orElse(Partner.ReArchCN_PartnerId);
        Partner partner   = partnerRepository.active(partnerId);
        Assert.notNull(partner, () -> new BusinessException(ResultCode.FAILURE, "未能找到合作伙伴![partnerId=" + partnerId + "]"));
        customer.setPartnerId(partnerId);
        customer.getCustomerTech().setPartner(partner);
    }
}